package com.jumper.cwolf.leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * 描述：
 * 1006. 笨阶乘
 * @author cwolf
 * @version 1.0
 * @date 2021/4/1 14:44
 */
public class Code220 {

    public static boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        if(nums.length<2){
            return false;
        }
        Map<Long,Long> map = new HashMap<>();
        int size = t+1;
        for(int i=0;i<nums.length;i++){
            long n = nums[i];
            long index = getIndex(n,size);
            if(map.containsKey(index)){
                return true;
            }
            map.put(index,n);
            long pre = index-1,next = index+1;
            if(map.containsKey(pre)&&n-map.get(pre)<=t){
                return true;
            }
            if(map.containsKey(next)&&map.get(next)-n<=t){
                return true;
            }
            System.out.println(index);
            //移除最早的一个数字
            if(i>=k){
                map.remove(getIndex(nums[i-k],size));
            }
        }


        return false;
    }


    public static  long getIndex(long n,int size){
        return n>=0?(n/size):(n+1)/size-1;
    }







    public static void main(String[] args) {
        System.out.println(Code220.containsNearbyAlmostDuplicate(new int[]{
               -3,3,-6
        },2,3));
    }


}
